import stack,props,items

class Game(object):
    'Represents the game environment. All GAME.DLL functions and vars are exported as members of this object.'

    def __init__(self):
        self.stack=stack.Stack()

    def FindItems(self,**kw):
        """ Helper function that returns a filter that finds items with specific attributes.

                FindItems(Type=123)                     # Find all items of type 123
                FindItems(ContID=game.BackpackID)       # Find all items in backpack
                FindItems(ContID=0,Dist=2)              # Find all items on the ground, EXACTLY 2 squares afrom player
                FindItems(ContID=0,Dist=(0,2))          # Find all items on the ground, up to 2 squares afrom player

            The returned object is an iterable (To use in for loops, etx) and can be reused.
        """
        return items.ItemsFilter(self,**kw)

    def Item(self,ID):
        'Returns an item object representing a given item (by ID)'
        for a in xrange(self.ScanItems(False)):
            r=self.GetItem(a)
            if r[0]==ID:
                return items.Item(self,*r)
        raise IndexError('Item Not Found')


class _Manager(object):
    'Helper singleton that is used to setup the Game class, more stuff will be added in the future (client selection, etc)'

    GetFeatures=props.QueryProp('GetFeatures')
    ListRVars=props.QueryProp('ListRVars')
    ListWVars=props.QueryProp('ListWVars')
    ListCmds=props.QueryProp('ListCmds')

    def __init__(self):
        self.stack=stack.Stack()
        self.Features=set(self.GetFeatures())

        rvars=set(self.ListRVars())
        wvars=self.ListWVars()
        wvars=dict((wvars[x],wvars[x+1]) for x in xrange(0,len(wvars),2))

        for var in rvars:
            if var in wvars:
                setattr(Game,var,props.WVarProp(var))
            else:
                setattr(Game,var,props.RVarProp(var))

        for cmd in self.ListCmds():
            setattr(Game,cmd,props.CallProp(cmd))


Manager=_Manager()

